package com.yjd.comm.base.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;

import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.StringUtil;

/**
 * @Description: 页面在JSTL中格式化Long类型的日期为想要的格式
 * @author SongJian devpage@126.com
 * @date 2015-4-29
 */
public class JSTLDateTag extends TagSupport {

	private static final long serialVersionUID = 1L;
	/**
	 * 默认时间格式yyyy-MM-dd
	 */
	private final String DEFAULT_DATE_PARTTERN = DateUtil.PATTERN_yyyy_MM_dd;

	private String value;

	public void setValue(String value)
	{
		this.value = value;
	}

	private String parttern = DEFAULT_DATE_PARTTERN;

	public void setParttern(String parttern)
	{
		this.parttern = parttern;
	}

	@Override
	public int doStartTag() throws JspException
	{
		try
		{ 
			if(StringUtils.isBlank(value)){
				return super.doStartTag();
			}
			String s = DateUtil.timestampToDateString(Long.valueOf(value),parttern);
			if(StringUtil.isNotEmpty(s)){
				pageContext.getOut().write(s);
			}else{
				pageContext.getOut().write("--");
			}
		} catch (IOException e)
		{
			throw new RuntimeException(e);
		}
		return super.doStartTag();
	}
 
}
